const axios = require('axios')
const Bagpipe = require('bagpipe')
const request = require('request')
const fs = require('fs')
const path = require('path')

const downLoadPic = function (src, dest) {
  request(src).pipe(fs.createWriteStream(dest).on('finish', function(){
    console.log('保存完成')
  }).on('error', function(err){
    console.log(err)
  }))
}

axios.get('https://pvp.qq.com/zlkdatasys/data_zlk_xpflby.json')
  .then(res => {
    // 得到图片地址集合
    const data = res.data.pcblzlby_c6
    // 循环遍历集合
    const bagpipe = new Bagpipe(2, { timeout: 1000})
    for(var i = 0 ; i < data.length; i++) {
      // console.log(data[i].pcblzlbydt_8b)
      // 得到的是一个图片地址 url -> 真实的图片
      // bagpipe.push(调用的函数, ...参数)
      bagpipe.push(downLoadPic, ('http:'+data[i].pcblzlbydt_8b), path.resolve(__dirname, `images/${data[i].pcblzlbybt_d3}.png`))
    }
  })
  .catch(err => {
    console.log(err)
  })